<template>
  <div class="singer" v-loading="!singers.length">
    <index-list :data="singers" @select="selectSinger"></index-list>
    <router-view v-slot="{ Component }" :selectSingerId="selectSingerId">
      <transition appear name="slide">
        <component :is="Component" :data="selectedSinger" />
      </transition>
    </router-view>
  </div>
</template>

<script>
import { getHotSinger } from "../api/index.js";
import { sortByTitle } from "@/assets/js/func";
import IndexList from "@/components/base/index-list/index-list.vue";
// import pinyin from "pinyin";
import { pinyin } from "pinyin-pro";
export default {
  data() {
    return {
      singers: [],
      selectSingerId: 0,
      selectedSinger: null,
    };
  },
  components: {
    IndexList,
  },
  async created() {
    const { data: singerResult } = await getHotSinger();
    //按汉字首字母构造
    const singerData = singerResult.artists.map((item) => {
      return {
        id: item.id,
        singerName: item.name,
        picUrl: item.picUrl,
        pinyin: pinyin(item.name)[0][0],
        title: pinyin(item.name, {
          style: pinyin.STYLE_NORMAL, // 设置拼音风格
        })[0][0]
          .slice(0, 1)
          .toUpperCase(),
      };
    });
    singerData.sort((a, b) => {
      return a.pinyin.localeCompare(b.pinyin);
    });
    //按title排序
    let resultSingerList = sortByTitle(singerData);
    this.singers = resultSingerList;
  },
  methods: {
    selectSinger(singer) {
      this.selectSingerId = singer.id;
      this.selectedSinger = singer;
      console.log(this.selectedSinger, "roture");
      this.$router.push({
        path: `/singer/${this.selectSingerId}`,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.singer {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  overflow: scroll;
}
</style>
